﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strom.Server.IO;

namespace Strom.Server.Commands
{
    public static class CommandParser
    {
        static readonly Dictionary<string,IConsoleCommand> Commands = new Dictionary<string, IConsoleCommand>(); 

        public static Command GetCommandDatas(string command)
        {
            if (command.Length == 0)
                return null;

            string[] datas = command.Contains(" ") ? command.Split(' ') : new[] {command};
            string[] parameters = new string[datas.Length - 1];

            Array.Copy(datas, 1, parameters, 0, parameters.Length);

            return new Command {CommandName = datas[0], Parameters = parameters};
      }

        public static void Parse(Command command)
        {
            if (command == null)
                return;

            if(!Commands.ContainsKey(command.CommandName))
            {
                ServerConsole.WriteSpecialMessageCommand("La commande " + command.CommandName + " est inconnue.", ServerConsole.MessageType.Fail);
                return;
            }

            Commands[command.CommandName].ExecuteWithParameters(command.Parameters);
        }

        static CommandParser()
        {
            Commands.Add(CreateAccountCommand.CommandName, new CreateAccountCommand());
            Commands.Add(ListAccountCommand.CommandName, new ListAccountCommand());
            Commands.Add(DeleteAccountCommand.CommandName, new DeleteAccountCommand());
            Commands.Add(HelpCommand.CommandName, new HelpCommand());
        }

        public static void DisplayHelp()
        {
            Console.WriteLine("\tCommandes disponibles:\n");

            foreach (var consoleCommand in Commands)
            {
                Console.WriteLine("\t" + consoleCommand.Key);
            }

            Console.WriteLine();
        }
    }
}
